﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Tzync
{
    /**************************************/
    /* Class to handle filters in Analyze */
    /**************************************/

    class Filter
    {

        private List<String> fileFilters;
        private List<String> AdvancedFilters;

        private bool includeFlag;

        public Filter()
        {
            includeFlag = true;
            fileFilters = new List<string>();
            AdvancedFilters = new List<string>();
        }

        public void IncludeFlag(bool includeFlag)
        {
            this.includeFlag = includeFlag;
        }
        public void AddFileFilter(String s)
        {
            fileFilters.Add(s);
        }

        public void PerformFilter(List<Difference> differences)
        {
            /*
            int i = 0, j = 0;
            bool matchFlag = false;
            string fileToBeCompared = null;

            for (i = 0; i < differences.Count; ++i)
            {

                if (differences[i].GetItemType() == Type.File && differences[i].GetPairType().First == DirectionType.SystemToHD)
                {

                    if (!isValid(differences[i].GetPartnerFile().getName(), differences[i].GetPartnerFile().getFullPathA()))
                    {
                         differences.RemoveAt(i);
                        --i;
                    } 

                }
            }
             */
        }


        public void flush()
        {
            fileFilters.RemoveRange(0, fileFilters.Count);
            AdvancedFilters.RemoveRange(0, AdvancedFilters.Count);
        }
        public int CountChar(string input, char c)
        {
            int retval = 0;
            for (int i = 0; i < input.Length; i++)
                if (c == input[i])
                    retval++;
            return retval;
        }
        public bool isValidAdvanced(string path)
        {
            if (AdvancedFilters.Count == 0)
                return true;
            if (includeFlag == true)
            {
                foreach (string advancedFilter in AdvancedFilters)
                {
                    if (path.Contains(advancedFilter) || advancedFilter.Contains(path))
                    {
                            if (path.Length > advancedFilter.Length)
                            {
                                if (path.ElementAt(advancedFilter.Length) != '\\') continue;
                            }
                            else if (path.Length < advancedFilter.Length)
                            {
                                if (advancedFilter.ElementAt(path.Length) != '\\') continue;
                            }


                                return true;
                    }
                }
                return false;
            }

            else
            {

                foreach (string advancedFilter in AdvancedFilters)
                {
                    if (advancedFilter.Contains(path))
                    {
                        if (advancedFilter != path)
                            if (advancedFilter.ElementAt(path.Length) != '\\')
                            continue;

                        return false;
                    }
                }
                return true;
 
            }

    }


        public bool isValid(string fileToBeCompared, string path)
        {
            if (fileFilters.Count == 0 && AdvancedFilters.Count==0)
                return true;

            bool flag = false;

            if (includeFlag == true)
            {
                flag = false;
                foreach (string advancedFilter in AdvancedFilters)
                {
                    if (path.Contains(advancedFilter))
                    {
                        if (advancedFilter!=path)
                            if(path.ElementAt(advancedFilter.Length)!='\\')
                            continue;

                        flag = true;
                    }
                }
            }

            else
            {
                flag = true;
                foreach (string advancedFilter in AdvancedFilters)
                {
                    if (path.Contains(advancedFilter))
                    {
                        if (advancedFilter != path)
                            if (path.ElementAt(advancedFilter.Length) != '\\')
                            continue;

                        flag = false;
                    }
                }
    
            }


            if (flag == false && AdvancedFilters.Count != 0 && fileFilters.Count == 0)
                return false;
            else if(AdvancedFilters.Count==0)
             flag = true;

            int i;
            bool ret = true;


            ret = !includeFlag;

            for (i = 0; i < fileFilters.Count; ++i)
                if (Regex.IsMatch(fileToBeCompared, WildcardToRegex(fileFilters[i])) == true)
                {
                    ret = includeFlag;
                    break;
                }
            if (flag && (ret || fileFilters.Count == 0) )
            return true;
            else
            return false;
            

        }

        public static string WildcardToRegex(string pattern)
        {
            return "^" + Regex.Escape(pattern).
            Replace("\\*", ".*").
            Replace("\\?", ".") + "$";
        }


        public void AddAdvancedFilter(string s)
        {
            AdvancedFilters.Add(s);
        }

        public  List<string> getFileFilters()
        {
            return fileFilters;
        }
        public List<string> getAdvancedFilters()
        {
            return AdvancedFilters;
        }
    }
}
